#!/usr/bin/perl
#index.cgi
use Data::Dumper;

require './dasman-lib.pl';

&ReadParse();

&header($text{'diskman_title'}, "", "index", 0, 1, undef, undef, "<meta http-equiv=refresh content=30>");


my (@luninfo, $d);

	@luninfo = get_luninfo(\@rdinfo);


print "<form name=editform action=index.cgi method=post onsubmit='return onsubmit_form(0,0,0,0);'>\n";

print "<table width=100% class=mode2><tr>\n";

print "<td  valign=top><table  class=mode1 border=0 cellspacing=1
	cellpadding=3 width=100%>\n";

print "<tr class=title1> <th align=left> $text{'lundev'}</th>",
	"<th align=left> $text{'luninfo'}</th>",
	"<th align=left> $text{'lunsize'}</th>",
	"<th align=left> $text{'lunmodel'}</th> </tr>\n",

my ($dsizeM, $i, $j,$found);
$i = 0;
foreach $d (@luninfo) {
	if(!$d->{'device'}){ next; }
	$i++;
	if ($i % 2 == 0) {
		$dismode = "class=mode2";
	}
	else {
		$dismode = "class=mode1";
	}

	$ispv = `lvm pvs |grep $d->{'device'}`;
	if($ispv){
		$pv = 1;
	}
	else{
		$pv = 0;
	}
	
	print "<tr $dismode>\n";
	print "<td nowarp $dismod width=20%>";
	print "<input type=radio name=disk value=$d->{'device'} ";
	print " onclick='checkpv($pv)'>";
	print  $d->{'device'};
	print  "</td>\n";
	print "<td nowarp $dismod width=20%>",
		$d->{'info'}, "</td>\n";
	if ($d->{'info'}) {
		print "<td nowarp $dismod width=20%>", $d->{'capacity'}, " GB", "</td>\n";
	}
	#print "<td nowarp $dismod width=20%>", $d->{'vendor'}, "</td>\n";
	if (!defined($d->{'model'})) {
		print "<td nowarp $dismod width=20%>",
		$text{'NA'}, "</td>\n";
	}
	else {
		print "<td nowarp $dismod width=20%>",
		$d->{'model'}, "</td>\n";
	}
	print "</tr>\n";
}

#print "<tr> <td colspan=5> <hr> </td> \n";

print "</td></tr></table>\n";

#print (format disk) button
$disable='disabled=true';
print "<td valign=top width=60>";
print "<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
print "<tr><td align=center><input type=submit name=dformat value=\"$text{'format'}\" onclick='return SetEditValue_disk(this,$i)' class=button disabled></td></tr>\n";
print "<tr><td align=center><input type=submit name=dscan onclick = 'return SetEditValue_disk(this, $i)' value=\"$text{'disk_rescan'}\" class=button></tr>\n";
print "</table></td>\n";


print "</table>\n";

print "<input type=hidden name=volname>\n";

print <<EOF;
<script>

function checkpv(mark)
{
	if (mark == 1) {
		document.editform.dformat.disabled=true;
	}
	else {
		document.editform.dformat.disabled=false;
	}
}

function onsubmit_form(vcreate_exist, vdetail_exist, vrecovery_exist, vdelete_exist)
{
        document.editform.dformat.disabled = true;
}


function SetEditValue_disk(obj, n)
{
	if (GetSelected_disk(obj, n))	{
		if (obj == document.editform.dformat) {
		} 
	}
	else if (obj != document.editform.dscan) {
		alert("$text{'noselectdisk'}");
		return false;
	}
	if (obj == document.editform.dformat) {
		document.editform.action = 'createpv.cgi';
	}
	else if (obj == document.editform.dhtswap) {
		document.editform.action = 'diskswap.cgi';
	}
	else if (obj == document.editform.dscan) {
		document.editform.action = 'index.cgi';
        }

	return true;
}

function GetSelected_disk(obj, n)
{
	var i;
	var found = false;

	if (n > 1) {
		for (i = 0; i < document.editform.disk.length; i++) {
			if (document.editform.disk[i].checked) {
				found = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.editform.disk.checked) {
			found = true;
		}
	}

	if (found) {
		if (n > 1)
			document.editform.disk.value = document.editform.disk[i].value;
		else
			document.editform.disk.value = document.editform.disk.value;
	}
	else
		document.editform.disk.value = '';
//	alert(document.editform.disk.value);
	return found;
}

</script>

EOF

print "</form>\n";
&footer("/", $text{'index'});

